<?php
use Mirage\Base\DB;
/**
 * Description of MovieModel
 * Date Created Mar 25, 2012 8:46:11 PM
 * @package 
 * @author Bryan Salazar
 * 
 */
class MovieModel {
    
    /**
     *
     * @var DB;
     */
    protected $db;


    public function __construct() {
        $this->db = DB::app('movie');
    }
    
    /**
     *
     * @var MovieModel
     */
    private static $_instance;
    
    /**
     * Get instance of MovieModel
     * @return MovieModel 
     */
    public static function app() {
        if(self::$_instance == null)
            self::$_instance = new MovieModel();
        return self::$_instance;
    }
    
    public function findAll() {
        $limit = $_GET['rows'];
        $sql = 'SELECT m.id, m.movie_title FROM movies m ' . 
            'INNER JOIN movie_types mt ON mt.movie_id = m.id ' . 
            'INNER JOIN ref_types rt ON mt.type_id = rt.id ' . 
            'GROUP BY m.id';
        
        $this->db->exec($sql);
        $data = $this->db->findAll();
        $this->db->exec('SELECT COUNT(*) totalrow FROM movies');
        $row=$this->db->find();
        if(!isset ($row['totalrow']))
            $row['totalrow'] = 0;
        return JqGrid::getJSON($row['totalrow'], $data,'',true);
    }
}
